﻿using System;
using System.Collections.Generic;
using System.Linq;
using Biz.EF.Platform.Gxt;
using log4net;


namespace Biz.Process
{
    public static class InfoBuffer
    {
        private static ILog Log = LogManager.GetLogger(typeof(InfoBuffer));
        private static List<companyinfo> _companyInfos = null;

        private static DateTime _lastLoadTime1 = DateTime.Now;
        public static companyinfo GetCompanyByMn(string mn, MnType mnType)
        {
            if (_companyInfos == null || (DateTime.Now - _lastLoadTime1).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformGxtContext())
                {
                    _companyInfos = pCtx.companyinfoes.ToList();
                }
                _lastLoadTime1 = DateTime.Now;
            }
            switch (mnType)
            {
                case MnType.Dcs:
                    return _companyInfos.FirstOrDefault(t => t.DcsMN.Equals(mn));
                case MnType.Total:
                    return _companyInfos.FirstOrDefault(t => t.TotalMN != null && t.TotalMN.Equals(mn));
                case MnType.Voc:
                    return _companyInfos.FirstOrDefault(t => t.VocMN.Equals(mn));
                case MnType.Gas:
                    return _companyInfos.FirstOrDefault(t => t.GasMN.Equals(mn));
                case MnType.Water:
                    return _companyInfos.FirstOrDefault(t => t.WaterMN.Equals(mn));
                case MnType.Monitor:
                    var item = _companyInfos.FirstOrDefault(t => t.GasMN.Equals(mn));
                    if (item != null && item.GasMN != null)
                    {
                        return item;
                    }
                    return _companyInfos.FirstOrDefault(t => t.WaterMN.Equals(mn));
                case MnType.Both:
                    item = _companyInfos.FirstOrDefault(t => t.GasMN.Equals(mn));
                    if (item != null && item.GasMN != null)
                    {
                        return item;
                    }
                    item = _companyInfos.FirstOrDefault(t => t.DcsMN.Equals(mn));
                    if (item != null && item.DcsMN != null)
                    {
                        return item;
                    }
                    item = _companyInfos.FirstOrDefault(t => t.VocMN.Equals(mn));
                    if (item != null && item.VocMN != null)
                    {
                        return item;
                    }
                    item = _companyInfos.FirstOrDefault(t => t.DcsMN.Equals(mn));
                    if (item != null && item.DcsMN != null)
                    {
                        return item;
                    }
                    return _companyInfos.FirstOrDefault(t => t.WaterMN.Equals(mn));
            }
            return null;
        }
    }
    public enum MnType
    {
        Dcs,
        Total,
        Voc,
        Monitor,
        Gas,
        Water,
        Both
    }
}
